using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace PackTool
{
    public abstract class TemplateDepHandler<T> : XMLHandler
    {
        protected Dictionary<string, T> mDepList;
        public TemplateDepHandler(Dictionary<string, T> list)
        {
            mDepList = list;
        }

        T current;

#if UNITY_EDITOR
        protected static string GetDepth(int depth)
        {
            string s = "";
            while ((depth--) > 0)
            {
                s += "  ";
            }

            return s;
        }

        public static string RootName = "Root";
        public delegate void WriterNode(string key, T v, StreamWriter Writer);
        public static WriterNode WriterFun;
        protected static void SaveToFile(Dictionary<string, T> DepList, string filename)
        {
            StreamWriter Write = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
            Write.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            Write.WriteLine("<{0}>", RootName);

            foreach (KeyValuePair<string, T> itor in DepList)
            {
                WriterFun(itor.Key, itor.Value, Write);
            }

            Write.WriteLine("</{0}>", RootName);
            Write.Close();
        }

        protected static void UpdateToFile(string name, T dep, string filename)
        {
            Dictionary<string, T> DepList = new Dictionary<string, T>();
            DepList[name] = dep;
            UpdateToFile(DepList, filename);
        }

        protected delegate void ReadXMLFile(MemoryStream Stream, Dictionary<string, T> depList);
        static protected ReadXMLFile ReadXML;
        protected static void UpdateToFile(Dictionary<string, T> DepList, string filename)
        {
            Dictionary<string, T> AssetDepList;
            if (File.Exists(filename))
            {
                FileStream file = null;
                try
                {
                    file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                    AssetDepList = new Dictionary<string, T>();
                    int size = (int)file.Length;
                    byte[] buffer = new byte[size];
                    file.Read(buffer, 0, size);
                    ReadXML(new MemoryStream(buffer), AssetDepList);
                    foreach (KeyValuePair<string, T> itor in DepList)
                    {
                        AssetDepList[itor.Key] = itor.Value;
                    }
                }
                catch (System.Exception e)
                {
                    AssetDepList = DepList;
                    UnityEngine.Debug.LogError(string.Format("UpdateToFile error!:{0}", e.Message));
                }

                if (file != null)
                    file.Close();
            }
            else
            {
                AssetDepList = DepList;
            }

            try
            {
                SaveToFile(AssetDepList, filename);
            }
            catch (System.Exception e)
            {
                UnityEngine.Debug.LogError(string.Format("SaveToFile error!:{0}", e.Message));
            }
        }
#endif
    }
}

